package dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import model.Aluno;

public class LoginDaoImpl implements LoginDao{

	Connection c;
	
	public LoginDaoImpl(){
		GenericDao gDao = new GenericDaoImpl();
		c = gDao.getConnection();
	}
	
	@Override
	public void cadastraSenha(Aluno aluno) throws SQLException {
		String sql = "{call sp_inseresenha(?,?)}";
		CallableStatement cs = c.prepareCall(sql);
		cs.setString(1, aluno.getRa());
		cs.setString(2, aluno.getSenha());
		cs.execute();
		cs.close();
	}

	@Override
	public boolean validaSenha(Aluno aluno) throws SQLException {
		boolean valido = false;
		String sql = "select dbo.fn_retornasenhavalida(?,?) as senha";
		PreparedStatement ps = c.prepareStatement(sql);
		ps.setString(1, aluno.getRa());
		ps.setString(2, aluno.getSenha());
		ResultSet rs = ps.executeQuery();
		if (rs.next()){
			valido = rs.getBoolean("senha");
		}
		return valido;
	}

}
